﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using XclusiveClient.Classes;

namespace XclusiveClient
{
    public partial class EditOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRetrieveById_Click(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:3676/");

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("api/order/" + tbxOrderID.Text).Result;
            if (response.IsSuccessStatusCode)
            {
                var o = response.Content.ReadAsAsync<Order>().Result;

                int status;
                if (o.status.Equals("Pending"))
                {
                    status = 0;
                }
                else
                {
                    status = 1;
                }
                rdbStatus.SelectedIndex = status;
                tbxShipDate.Text = o.ship_date.ToString();
                ddlShipTime.SelectedItem.Text = o.ship_time.ToString();
                ddlShipType.SelectedItem.Text = o.ship_type.ToString();
                tbxAddress.Text = o.address.ToString();
                tbxEmpID.Text = o.employeeID.ToString();
                tbxListID.Text = o.listID.ToString();
            }
            else
            {
                LblResult.Text += "Could not retrieve Order. Error code:" + response.StatusCode + "reason:" + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            var order = new Order()
            {
                status = rdbStatus.SelectedItem.Text,
                ship_date = tbxShipDate.Text,
                ship_time = ddlShipTime.SelectedItem.Text,
                ship_type = ddlShipType.Text,
                address = tbxAddress.Text,
                employeeID = tbxEmpID.Text,
                listID = Convert.ToInt32(tbxListID.Text)
            };

            HttpResponseMessage response = client.PutAsJsonAsync("api/Order/" + tbxOrderID.Text, order).Result;

            if (response.IsSuccessStatusCode)
            {
                Response.Redirect("EditOrderSuccess.aspx");
            }
            else
            {
                LblResult.Text = "Unable to update Order. Error code: " + response.StatusCode + ", Reason:" + response.ReasonPhrase.ToString() + "<br/>";
            }
        }
    }
}